import { createStore } from './createStore'

export const useRouteStore = createStore('route', {
  state: {
    routes: [],
    lastFetch: null,
    cacheTimeout: 3600 * 1000 // 1小时缓存
  },
  persist: {
    enable: true,
    keys: ['routes', 'lastFetch']
  },
  getters: {
    isRoutesExpired() {
      if (!this.lastFetch) return true
      return Date.now() - this.lastFetch > this.cacheTimeout
    }
  },
  actions: {
    setRoutes(routes) {
      this.routes = routes
      this.lastFetch = Date.now()
    },
    clearRoutes() {
      this.routes = []
      this.lastFetch = null
    },
    getRouteByPath(path) {
      return this.routes.find(route => route.path === path)
    }
  }
})